<!DOCTYPE html> 
<html> 
<head> 
  <script src="../../../processing.js"></script> 
  <script src="../../init.js"></script> 
<link rel="stylesheet" href="../style.css"/></head> 
<body><h1><a href="http://asalga.wordpress.com">Andor Salga</a></h1> 
<h2>sort() test</h2> 
 
<p>This page tests to make sure my implementation of <a href="http://www.processing.org/reference/sort_.html">sort</a> works for the<br /> 
<a href="http://processingjs.org/">Processing.js</a> library.
</p> 
 
<p> 
 
<script type="application/processing"> 
 
int testsPassed = 0;
boolean failed = false;
 
PFont font = loadFont("courier");
textFont(font);
 
void setup()
{
  size(400,400);
  fill(0);
  
  // FLOATS
  float[] f1 = { 3.4, 3.6, 2, 0, 7.1 };
  float[] rf1 = {0.0, 2.0, 3.4, 3.6, 7.1};
  
  float[] f2 = {0.0};
  float[] rf2 = {0.0};
  
  float[] f3 = {5,4,3,2,1};
  float[] rf3 = {1,2,3,4,5};
  
  float[] f4 = {4.4, 3.3, 2.2, 1.1};
  float[] rf4 = {1.1, 2.2, 3.3, 4.4};
  
  float[] f5_4 = {6.6, 4.4, 3.3, 9.9, 7.7, 4.4, 1.1};
  float[] rf5_4 = {3.3, 4.4, 6.6, 9.9, 7.7, 4.4, 1.1};
 
  float[] f6_1 = {6.6, 4.4, 3.3, 9.9, 7.7, 4.4, 1.1};
  float[] rf6_1 = {6.6, 4.4, 3.3, 9.9, 7.7, 4.4, 1.1 };
  
  float[] f7_5 = {4.4, 3.3, 1.1, 6.6, 8.8};
  float[] rf7_5 = {1.1, 3.3, 4.4, 6.6, 8.8};
  
  float[] f8 = {1,2,3,4};
  float[] rf8 = {1,2,3,4};
  
  float[] f9_3 = {1,2,3,4,5};
  float[] rf9_3 = {1,2,3,4,5};
  
  float[] f10 = { 7.7, 3.1, 9.9, 6.5, 9.8, 3.14, 3.4, 5.6};
  float[] rf10 = {3.1, 3.14, 3.4, 5.6, 6.5, 7.7, 9.8, 9.9};
  
  float[] f11 =  {9.0, -7.3, 0.0, 5.1, 9.34, 2.8, 1, 8, 3, 0};
  float[] rf11 = {-7.3, 0.0, 0.0, 1.0, 2.8, 3.0, 5.1, 8.0, 9.0, 9.34};
  
  float[] f12 = {};
  float[] rf12 =  {};
  
  float[] f13_9 =  {8,4,6,7,9,0,-9,2,4,1,1,1,1,1,1,1,1};
  float[] rf13_9 = {-9,0,2,4,4,6,7,8,9,1,1,1,1,1,1,1,1};
  
  // INTS
  int[] i1 = {1,2,3,4,5};
  int[] ri1 = {1,2,3,4,5};
  
  int[] i2_3 = {1,2,3,4,5};
  int[] ri2_3 = {1,2,3,4,5};
  
  int[] i3 = {5,4,3,2,1};
  int[] ri3 = {1,2,3,4,5};
  
  int[] i4 =  {2,7,4,8,1,2,9};
  int[] ri4 = {1,2,2,4,7,8,9};
  
  int[] i5_4 = {2,7,4,8,1,2,9};
  int[] ri5_4 = {2,4,7,8,1,2,9};
  
  int[] i6 = {4,3,1,2,3,4,1,2,2,3,4,1};
  int[] ri6 = {1,1,1,2,2,2,3,3,3,4,4,4};
  
  int[] i7 = {1,1,1};
  int[] ri7 = {1,1,1};
  
  int[] i8_3 = {1,2,1,5,6,7};
  int[] ri8_3 = {1,1,2,5,6,7};
  
  int[] i9 = {0};
  int[] ri9 = {0};
  
  int[] i10 = {};
  int[] ri10 = {};
  
  int[] i11_8 = {5,1,9,7,3,4,5,6,1};
  int[] ri11_8 = {1,3,4,5,5,6,7,9,1};
  
  int[][] i12 = {{3,2,1},{4,5,6}};
  int[] ri12 = {1,2,3};
  
  int[][] i13_5 = {{5,8,1,3,9},{4,7,8,9,2},{3,1,7,9,3},{7,4,9,7,9,0,3}};
  int[] ri13_5 = {4,7,7,9,9,0,3};
  
  // STRINGS
  String[] s1 =  {"deer", "elephant", "bear", "aardvark", "cat"};
  String[] rs1 = {"aardvark", "bear", "cat", "deer", "elephant"};
   
  String[] s2_3 =  {"deer", "elephant", "bear", "aardvark", "cat" };
  String[] rs2_3 = {"bear", "deer", "elephant", "aardvark", "cat" };
  
  String[] s3 =  {"A", "B", "C", "D"};
  String[] rs3 = {"A", "B", "C", "D"};
  
  String[] s4 =  {"Z","Y","X"};
  String[] rs4 = {"X","Y","Z"};
  
  String[] s5 =  {"A", "b", "C", "a", "c", "B"};
  String[] rs5 =  {"A", "B", "C", "a", "b", "c"}; //case sensitive
  //String[] rs5 = {"A", "a", "b", "B", "C", "c"}; case insensitive
 
  String[] s6 =  {"Andor","DPS909","andor","salga","Salga"};
  String[] rs6 = {"Andor","DPS909","Salga","andor","salga"};
  //String[] rs6 = {"Andor","andor","DPS909","salga","Salga"}; 
 
  String[] s7 =  {"abc","def","xyz","qrs","ghi","lmn"};
  String[] rs7 = {"abc","def","ghi","lmn","qrs","xyz"};
 
  String[] s8_4 =  {"abc","def","xyz","qrs","ghi","lmn"};
  String[] rs8_4 = {"abc","def","qrs","xyz","ghi","lmn"};
  
  String[][] s9 = {
                    {"ABC","YYX"},
                     {"123","456","993","42","67"},
                    {"XYZ"}
                  };
 String[]  rs9 = {"123","42","456","67","993"}; 
 
 String[] s10 =  {"#","*","@","(","$","a","5","~"};
 String[] rs10 = {"#","$","(","*","5","@","a","~"};
 
 String[] s11 = {};
 String[] rs11 = {};
 
 String[] s12 ={"6.3","1.7","-9.5","2.5","4.4","1.5", "7", "0.5","-8.4"};
 String[] rs12 = {"-8.4", "-9.5", "0.5", "1.5","1.7","2.5", "4.4", "6.3", "7"};
 
 String[] s13 = {"a"};
 String[] rs13 = {"a"};
 
 String[] s14 = {"a","b"};
 String[] rs14 = {"a","b"};
 
 String[] s15 = {"b","a"};
 String[] rs15 = {"a","b"};
 
 String[] s16 = {"a","b","c","b","a"};
 String[] rs16 = {"a","a","b","b","c"};
 
  if( 
      arraysEquals(sort(s1), rs1) &&
      arraysEquals(sort(s2_3,3), rs2_3) &&
      arraysEquals(sort(s3), rs3) &&
      arraysEquals(sort(s4), rs4) &&
      arraysEquals(sort(s5), rs5) &&
      arraysEquals(sort(s6), rs6) &&
      arraysEquals(sort(s7), rs7) &&
      arraysEquals(sort(s8_4,4), rs8_4) &&
      arraysEquals(sort(s9[1]), rs9) &&
      arraysEquals(sort(s10), rs10) &&
      arraysEquals(sort(s11), rs11) &&
      arraysEquals(sort(s12), rs12) &&
      arraysEquals(sort(s13), rs13) &&
      arraysEquals(sort(s14), rs14) &&
      arraysEquals(sort(s15), rs15) &&
      arraysEquals(sort(s16), rs16) &&
	  	  
      arraysEquali(sort(i1), ri1) &&
      arraysEquali(sort(i2_3), ri2_3) &&
      arraysEquali(sort(i3), ri3) &&
      arraysEquali(sort(i4), ri4) &&
      arraysEquali(sort(i5_4,4), ri5_4) &&
      arraysEquali(sort(i6), ri6) &&
      arraysEquali(sort(i7), ri7) &&
      arraysEquali(sort(i8_3), ri8_3) &&
      arraysEquali(sort(i9), ri9) &&
      arraysEquali(sort(i10), ri10) &&
      arraysEquali(sort(i11_8,8), ri11_8) &&
      arraysEquali(sort(i12[0]), ri12) &&
      arraysEquali(sort(i13_5[3],5), ri13_5) &&
		
      // FLOATS
      arraysEqualf(sort(f1), rf1) &&
      arraysEqualf(sort(f2), rf2) &&
      arraysEqualf(sort(f3), rf3) &&
      arraysEqualf(sort(f4), rf4) &&
      arraysEqualf(sort(f5_4, 4), rf5_4) &&
      arraysEqualf(sort(f6_1, 1), rf6_1) &&
      arraysEqualf(sort(f7_5, 5), rf7_5) &&
      arraysEqualf(sort(f8), rf8) &&
      arraysEqualf(sort(f9_3), rf9_3) &&
      arraysEqualf(sort(f10), rf10) &&
      arraysEqualf(sort(f11), rf11) &&
      arraysEqualf(sort(f12), rf12) &&
      arraysEqualf(sort(f13_9,9), rf13_9) 
	  )
	 {
		text("All " + testsPassed + " tests have passed.",20,20)
	 }
     else
     {
	   text("failed on test " + (testsPassed+1), 20, 20);
    }
 
}
 
void printArray(arr)
{
	for(var i =0 ; i < arr.length; i++)
	{
		text("[" + i + "]" + " " + arr[i], 20, (i*10) +40);
	}
}
 
boolean arraysEquali(int[] i1, int[] i2)
{
  if(!failed && i1.length != i2.length)
  {
    return false;
  }
  
  for(int i = 0; i < i1.length; i++)
  {
     if(i1[i] != i2[i]) return false;
  }
  testsPassed++;
  return true;
}
 
boolean arraysEqualf(float[] f1, float[] f2)
{
  if(!failed && f1.length != f2.length)
  {
    return false;
  }
  
  for(int i=0; i< f1.length; i++)
  {
    if(f1[i] != f2[i])
	{
      return false;
    }
  }
 
  testsPassed++;
  return true;
}
 
boolean stringsEqual(String str1, String str2)
{
  if(str1.length() != str2.length())
  {
    return false;
  }
 
  for(int i = 0; i < str1.length(); i++)
  {
    char ai = str1[i];
    char bi = str2[i];
		
    if( ai != bi )
    {
      return false;
    }
  }
  return true;
}
 
boolean arraysEquals(String[] a, String[] b)
{
  if(!failed && a.length != b.length) 
  {
    return false;
  }
   
  for(int i=0; i < a.length; i++)
  {    
    if(stringsEqual(a[i],b[i]) == false )
    {
      return false;
    }    
  }
 
   testsPassed++;
   return true;
}
</script><canvas style="border:1px solid black" width="400" height="400"></canvas></p> 
<div style="height:0px;width:0px;overflow:hidden;"></div> 
 
<pre><b>//Test written by <a href="http://asalga.wordpress.com/">Andor Salga</a></b> 
int testsPassed = 0;
boolean failed = false;
 
PFont font = loadFont("courier");
textFont(font);
 
void setup()
{
    size(400,400);
    fill(0);
    
    // FLOATS
    float[] f1 = { 3.4, 3.6, 2, 0, 7.1 };
    float[] rf1 = {0.0, 2.0, 3.4, 3.6, 7.1};
    
    float[] f2 = {0.0};
    float[] rf2 = {0.0};
    
    float[] f3 = {5,4,3,2,1};
    float[] rf3 = {1,2,3,4,5};
    
    float[] f4 = {4.4, 3.3, 2.2, 1.1};
    float[] rf4 = {1.1, 2.2, 3.3, 4.4};
    
    float[] f5_4 = {6.6, 4.4, 3.3, 9.9, 7.7, 4.4, 1.1};
    float[] rf5_4 = {3.3, 4.4, 6.6, 9.9, 7.7, 4.4, 1.1};
    
    float[] f6_1 = {6.6, 4.4, 3.3, 9.9, 7.7, 4.4, 1.1};
    float[] rf6_1 = {6.6, 4.4, 3.3, 9.9, 7.7, 4.4, 1.1 };
    
    float[] f7_5 = {4.4, 3.3, 1.1, 6.6, 8.8};
    float[] rf7_5 = {1.1, 3.3, 4.4, 6.6, 8.8};
    
    float[] f8 = {1,2,3,4};
    float[] rf8 = {1,2,3,4};
    
    float[] f9_3 = {1,2,3,4,5};
    float[] rf9_3 = {1,2,3,4,5};
    
    float[] f10 = { 7.7, 3.1, 9.9, 6.5, 9.8, 3.14, 3.4, 5.6};
    float[] rf10 = {3.1, 3.14, 3.4, 5.6, 6.5, 7.7, 9.8, 9.9};
    
    float[] f11 =  {9.0, -7.3, 0.0, 5.1, 9.34, 2.8, 1, 8, 3, 0};
    float[] rf11 = {-7.3, 0.0, 0.0, 1.0, 2.8, 3.0, 5.1, 8.0, 9.0, 9.34};
    
    float[] f12 = {};
    float[] rf12 =  {};
    
    float[] f13_9 =  {8,4,6,7,9,0,-9,2,4,1,1,1,1,1,1,1,1};
    float[] rf13_9 = {-9,0,2,4,4,6,7,8,9,1,1,1,1,1,1,1,1};
    
    // INTS
    int[] i1 = {1,2,3,4,5};
    int[] ri1 = {1,2,3,4,5};
    
    int[] i2_3 = {1,2,3,4,5};
    int[] ri2_3 = {1,2,3,4,5};
    
    int[] i3 = {5,4,3,2,1};
    int[] ri3 = {1,2,3,4,5};
    
    int[] i4 =  {2,7,4,8,1,2,9};
    int[] ri4 = {1,2,2,4,7,8,9};
    
    int[] i5_4 = {2,7,4,8,1,2,9};
    int[] ri5_4 = {2,4,7,8,1,2,9};
    
    int[] i6 = {4,3,1,2,3,4,1,2,2,3,4,1};
    int[] ri6 = {1,1,1,2,2,2,3,3,3,4,4,4};
    
    int[] i7 = {1,1,1};
    int[] ri7 = {1,1,1};
    
    int[] i8_3 = {1,2,1,5,6,7};
    int[] ri8_3 = {1,1,2,5,6,7};
    
    int[] i9 = {0};
    int[] ri9 = {0};
    
    int[] i10 = {};
    int[] ri10 = {};
    
    int[] i11_8 = {5,1,9,7,3,4,5,6,1};
    int[] ri11_8 = {1,3,4,5,5,6,7,9,1};
    
    int[][] i12 = {{3,2,1},{4,5,6}};
    int[] ri12 = {1,2,3};
    
    int[][] i13_5 = {{5,8,1,3,9},{4,7,8,9,2},{3,1,7,9,3},{7,4,9,7,9,0,3}};
    int[] ri13_5 = {4,7,7,9,9,0,3};
    
    // STRINGS
    String[] s1 =  {"deer", "elephant", "bear", "aardvark", "cat"};
    String[] rs1 = {"aardvark", "bear", "cat", "deer", "elephant"};
    
    String[] s2_3 =  {"deer", "elephant", "bear", "aardvark", "cat" };
    String[] rs2_3 = {"bear", "deer", "elephant", "aardvark", "cat" };
    
    String[] s3 =  {"A", "B", "C", "D"};
    String[] rs3 = {"A", "B", "C", "D"};
    
    String[] s4 =  {"Z","Y","X"};
    String[] rs4 = {"X","Y","Z"};
    
    String[] s5 =  {"A", "b", "C", "a", "c", "B"};
    String[] rs5 =  {"A", "B", "C", "a", "b", "c"}; //case sensitive
    //String[] rs5 = {"A", "a", "b", "B", "C", "c"}; case insensitive
    
    String[] s6 =  {"Andor","DPS909","andor","salga","Salga"};
    String[] rs6 = {"Andor","DPS909","Salga","andor","salga"};
    //String[] rs6 = {"Andor","andor","DPS909","salga","Salga"};
    
    String[] s7 =  {"abc","def","xyz","qrs","ghi","lmn"};
    String[] rs7 = {"abc","def","ghi","lmn","qrs","xyz"};
    
    String[] s8_4 =  {"abc","def","xyz","qrs","ghi","lmn"};
    String[] rs8_4 = {"abc","def","qrs","xyz","ghi","lmn"};
    
    String[][] s9 = {
        {"ABC","YYX"},
        {"123","456","993","42","67"},
        {"XYZ"}
    };
    String[]  rs9 = {"123","42","456","67","993"};
    
    String[] s10 =  {"#","*","@","(","$","a","5","~"};
    String[] rs10 = {"#","$","(","*","5","@","a","~"};
    
    String[] s11 = {};
    String[] rs11 = {};
    
    String[] s12 ={"6.3","1.7","-9.5","2.5","4.4","1.5", "7", "0.5","-8.4"};
    String[] rs12 = {"-8.4", "-9.5", "0.5", "1.5","1.7","2.5", "4.4", "6.3", "7"};
    
    String[] s13 = {"a"};
    String[] rs13 = {"a"};
    
    String[] s14 = {"a","b"};
    String[] rs14 = {"a","b"};
    
    String[] s15 = {"b","a"};
    String[] rs15 = {"a","b"};
    
    String[] s16 = {"a","b","c","b","a"};
    String[] rs16 = {"a","a","b","b","c"};
    
    if(
    arraysEquals(sort(s1), rs1) &&
    arraysEquals(sort(s2_3,3), rs2_3) &&
    arraysEquals(sort(s3), rs3) &&
    arraysEquals(sort(s4), rs4) &&
    arraysEquals(sort(s5), rs5) &&
    arraysEquals(sort(s6), rs6) &&
    arraysEquals(sort(s7), rs7) &&
    arraysEquals(sort(s8_4,4), rs8_4) &&
    arraysEquals(sort(s9[1]), rs9) &&
    arraysEquals(sort(s10), rs10) &&
    arraysEquals(sort(s11), rs11) &&
    arraysEquals(sort(s12), rs12) &&
    arraysEquals(sort(s13), rs13) &&
    arraysEquals(sort(s14), rs14) &&
    arraysEquals(sort(s15), rs15) &&
    arraysEquals(sort(s16), rs16) &&
    
    arraysEquali(sort(i1), ri1) &&
    arraysEquali(sort(i2_3), ri2_3) &&
    arraysEquali(sort(i3), ri3) &&
    arraysEquali(sort(i4), ri4) &&
    arraysEquali(sort(i5_4,4), ri5_4) &&
    arraysEquali(sort(i6), ri6) &&
    arraysEquali(sort(i7), ri7) &&
    arraysEquali(sort(i8_3), ri8_3) &&
    arraysEquali(sort(i9), ri9) &&
    arraysEquali(sort(i10), ri10) &&
    arraysEquali(sort(i11_8,8), ri11_8) &&
    arraysEquali(sort(i12[0]), ri12) &&
    arraysEquali(sort(i13_5[3],5), ri13_5) &&
    
    // FLOATS
    arraysEqualf(sort(f1), rf1) &&
    arraysEqualf(sort(f2), rf2) &&
    arraysEqualf(sort(f3), rf3) &&
    arraysEqualf(sort(f4), rf4) &&
    arraysEqualf(sort(f5_4, 4), rf5_4) &&
    arraysEqualf(sort(f6_1, 1), rf6_1) &&
    arraysEqualf(sort(f7_5, 5), rf7_5) &&
    arraysEqualf(sort(f8), rf8) &&
    arraysEqualf(sort(f9_3), rf9_3) &&
    arraysEqualf(sort(f10), rf10) &&
    arraysEqualf(sort(f11), rf11) &&
    arraysEqualf(sort(f12), rf12) &&
    arraysEqualf(sort(f13_9,9), rf13_9)
    )
    {
        text("All " + testsPassed + " tests have passed.",20,20)
    }
    else
    {
        text("failed on test " + (testsPassed+1), 20, 20);
    }
    
}
 
void printArray(arr)
{
    for(var i =0 ; i &lt; arr.length; i++)
    {
        text("[" + i + "]" + " " + arr[i], 20, (i*10) +40);
    }
}
 
boolean arraysEquali(int[] i1, int[] i2)
{
    if(!failed && i1.length != i2.length)
    {
        return false;
    }
    
    for(int i = 0; i &lt; i1.length; i++)
    {
        if(i1[i] != i2[i]) return false;
    }
    testsPassed++;
    return true;
}
 
boolean arraysEqualf(float[] f1, float[] f2)
{
    if(!failed && f1.length != f2.length)
    {
        return false;
    }
    
    for(int i=0; i&lt; f1.length; i++)
    {
        if(f1[i] != f2[i])
        {
            return false;
        }
    }
    
    testsPassed++;
    return true;
}
 
boolean stringsEqual(String str1, String str2)
{
    if(str1.length() != str2.length())
    {
        return false;
    }
    
    for(int i = 0; i &lt; str1.length(); i++)
    {
        char ai = str1[i];
        char bi = str2[i];
        
        if( ai != bi )
        {
            return false;
        }
    }
    return true;
}
 
boolean arraysEquals(String[] a, String[] b)
{
    if(!failed && a.length != b.length)
    {
        return false;
    }
    
    for(int i=0; i &lt; a.length; i++)
    {
        if(stringsEqual(a[i],b[i]) == false )
        {
            return false;
        }
    }
    
    testsPassed++;
    return true;
}</pre> 
 
 
<span id = 'debug'></span> 
</body> 
</html>